<!DOCTYPE html>
<html xmlns:shiro="http://www.pollix.at/thymeleaf/shiro" lang="zh" xmlns:th="http://www.thymeleaf.org">
<body  class="list" >
<div th:replace="include/head.html::html"></div>

<div class="layui-fluid  layui-anim-upbit layui-anim">
    <div class="layui-card">
        <!--表格中的数据 并不显示-->
        <div class="layui-card-body">
            <!--数据表格 -->
            <table class="layui-hidden" onscroll="scroll()" id="treeTable" lay-filter="treeTable"></table>
        </div>
    </div>
</div>
<script type="text/javascript" th:inline="none">
    var editObj = null, ptable = null, treeGrid = null, tableId = 'treeTable', layer = null;
    var a = 0;
    layui.use(['jquery', 'treeGrid', 'layer'], function () {
        var $ = layui.jquery;
        treeGrid = layui.treeGrid;//很重要
        layer = layui.layer;
        ptable = treeGrid.render({
            id: tableId
            , elem: '#' + tableId
            , url: 'permission/json'
            , cellMinWidth: 100
            , skin: 'line'
            , idField: 'oid'//必須字段
            , treeId: 'oid'//树形id字段名称
            , treeUpId: 'parentId'//树形父id字段名称
            , treeShowName: 'title'//以树形式显示的字段
            , heightRemove: [".dHead", 0]//不计算的高度,表格设定的是固定高度，此项不生效
            , height: 'full-50'
            , isFilter: false
            , iconOpen: true//是否显示图标【默认显示】
            , isOpenDefault: true//节点默认是展开还是折叠【默认展开】
            , loading: true
            , method: 'post'
            , isPage: false
            , cols: [[
                {type: 'numbers'}
                , {field: 'title', title: '标题'}
                , {field: 'url', title: '链接'}
                , {field: 'permission', title: '权限'}
                , {field: 'sortNum', title: '排序'}
                , {
                    title: '类型', align: 'center'
                    , templet: function (d) {
                        if (d.type === 1) {
                            return "<span>列表</span>";
                        } else if (d.type === 2) {
                            return "<span>权限</span>";
                        } else {
                            return "";
                        }
                    }
                }
                , {
                    title: '操作', align: 'center'
                    , templet: function (d) {
                        if (d.oid === '000') {
                            return '<a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="add">添加</a>';
                        } else {
                            var addBtn = '<a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="add">添加</a>';
                            var editBtn = '<a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">编辑</a>';
                            var delBtn = '<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>';
                            return addBtn + editBtn + delBtn;
                        }
                    }
                }
            ]]
            , parseData: function (res) {//数据加载后回调
                return res;
            }
            ,done:function () {
                $(".layui-table-main").animate({scrollTop:a},300);
                $('.layui-table-main').scroll(function (res) {
                    a = $(this).scrollTop();
                })
            }
            , onClickRow: function (index, o) {
                //console.log(index,o,"单击！");
            }
            , onDblClickRow: function (index, o) {
                //console.log(index,o,"双击");
            }
            , onCheck: function (obj, checked, isAll) {//复选事件
                //console.log(obj,checked,isAll);
            }
            , onRadio: function (obj) {//单选事件
                //console.log(obj);
            }
        });

        treeGrid.on('tool(' + tableId + ')', function (obj) {
            if (obj.event === 'del') {//删除行
                del(obj);
            } else if (obj.event === "add") {//添加行
                add(obj);
            } else if (obj.event === "edit") {//编辑
                edit(obj);
            }
        });
    });

    function del(obj) {
        layer.confirm("你确定删除数据吗？如果存在下级节点则一并删除，此操作不能撤销！", {icon: 3, title: '提示'},
            function (index) {//确定回调
                $.post("permission/del", {oid: obj.data.oid}, function (res) {
                    if (res.code === 0) {
                        layer.msg(res.msg);
                        layer.close(index);
                        obj.del();
                    } else {
                        layer.msg(res.msg);
                    }
                });
                layer.close(index);
            }, function (index) {//取消回调
                layer.close(index);
            }
        );
    }

    //添加
    function add(obj) {
        var index = editLayer({
            title: '添加权限',
            url: 'permission/edit?parentId=' + obj.data.oid,
            area: ['1000px', '600px'],
            back: callback,
            param: obj.data[treeGrid.config.indexName] + 1
        });
    }

    function edit(obj) {
        var index = editLayer({
            title: '编辑权限',
            url: 'permission/edit?oid=' + obj.data.oid,
            area: ['1000px', '600px'],
            back: callbackedit,
            param: obj
        });
    }

    function callback(ifr,index, data, param) {
        var field = data.field;
        $.post("permission/saveOrUpdate", field, function (res) {
            if (res.code === 0) {
                topLayer.msg(res.msg);
                topLayer.close(index);
                treeGrid.reload(tableId);
            } else {
                topLayer.msg(res.msg);
            }
        });
    }

    function callbackedit(ifr,index, data, param) {
        var field = data.field;
        $.post("permission/saveOrUpdate", field, function (res) {
            if (res.code === 0) {
                topLayer.msg(res.msg);
                topLayer.close(index);
                treeGrid.reload(tableId);
            } else {
                topLayer.msg(res.msg);
            }
        });
    }
</script>
</body>
</html>
